﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing.Design;
using System.Windows.Forms.Design;
using System.Windows.Forms;
using System.Diagnostics;
using UIDesigner.Designers.Controls;
using UIDesigner.DesignContainers.Bases;
using UIDesigner.SurfaceProperties;
using System.Drawing;

namespace UIDesigner.Editors
{
    public class DynamicEnumType
    {
        private Type m_objEnumType;
        private int m_nCurrentValue;
        private AFDesignerHost m_objHost;
        private IBaseObjectControl m_objControl;

        public DynamicEnumType(AFDesignerHost objHost, IBaseObjectControl objControl, Type objEnumType, int nCurrentValue)
        {
            m_objEnumType = objEnumType;
            m_nCurrentValue = nCurrentValue;
            m_objHost = objHost;
            m_objControl = objControl;
        }

        public IBaseObjectControl BaseObjectControl
        {
            get { return m_objControl; }
        }

        public AFDesignerHost Host
        {
            get { return m_objHost; }
        }

        public int CurrentValue
        {
            get 
            { 
                return m_nCurrentValue; 
            }
        }

        public Type EnumType
        {
            get 
            { 
                return m_objEnumType; 
            }
        }

        public override string ToString()
        {
            return Enum.GetName(m_objEnumType, m_nCurrentValue); 
        }
    }

    public class DynamicEnumEditor : UITypeEditor
    {
        private Rectangle m_rcList;

        public override UITypeEditorEditStyle GetEditStyle(System.ComponentModel.ITypeDescriptorContext context)
        {
            return UITypeEditorEditStyle.DropDown;
        }

        public override bool IsDropDownResizable
        {
            get
            {
                return false;
            }
        }

        public override object EditValue(System.ComponentModel.ITypeDescriptorContext context, IServiceProvider provider, object value)
        {
            IWindowsFormsEditorService editorService = (IWindowsFormsEditorService) provider.GetService(typeof(IWindowsFormsEditorService));
            DynamicEnumType objDynamicEnumType = (DynamicEnumType) value;

             if (editorService != null)
             {
                 ListBox lstValues = new ListBox();
                 bool bHasExisting = false;
                 string strName;

                 lstValues.BorderStyle = BorderStyle.None;
                 lstValues.Tag = editorService;

                 switch (objDynamicEnumType.EnumType.Name)
                 {
                     case "CreateOptions":
                         bHasExisting = objDynamicEnumType.Host.CreateButtonsExist;
                         break;
                     case "DeleteOptions":
                         bHasExisting = objDynamicEnumType.Host.DeleteButtonsExist;
                         break;
                     case "ResetOptions":
                         bHasExisting = objDynamicEnumType.Host.ClearButtonsExist;
                         break;
                     case "SaveOptions":
                         bHasExisting = objDynamicEnumType.Host.SaveButtonsExist;
                         break;
                     default:
                         Debugger.Break();
                         break;
                 }

                 foreach (object objValue in Enum.GetValues(objDynamicEnumType.EnumType))
                 {
                     strName = Enum.GetName(objDynamicEnumType.EnumType, objValue);

                     if (strName.StartsWith("UseExisting"))
                     {
                         if (bHasExisting)
                         {
                             lstValues.Items.Add(objValue);
                         }
                     }
                     else
                     {
                         lstValues.Items.Add(objValue);
                     }
                 }

                 lstValues.SelectedIndexChanged += new EventHandler(SelectedIndexChanged);
                 lstValues.ParentChanged += new EventHandler(lstValues_ParentChanged);

                 editorService.DropDownControl(lstValues);

                 if (objDynamicEnumType.EnumType.Name == "CreateOptions")
                 {
                     if (lstValues.SelectedItem != null)
                     {
                         strName = Enum.GetName(objDynamicEnumType.EnumType, lstValues.SelectedItem);

                         if (strName != "None")
                         {
                             ctrlCreateNewWizard ctrl = new ctrlCreateNewWizard(objDynamicEnumType.Host, objDynamicEnumType.BaseObjectControl, m_rcList, editorService, (CreateOptions)lstValues.SelectedItem);

                             try
                             {
                                 editorService.DropDownControl(ctrl);
                             }
                             catch (Exception e)
                             {
                                 Debugger.Break(); 
                             }

                             if (ctrl.DialogResult == DialogResult.Cancel)
                             {
                                 return objDynamicEnumType.CurrentValue;
                             }
                             else
                             {
                                 objDynamicEnumType.BaseObjectControl.HandleSurfaceProperties(objDynamicEnumType);
                                 return lstValues.SelectedItem;
                             }
                         }
                     }
                 }

                 // Return the new property value from the UI editor control
                 return lstValues.SelectedItem;
             }
             else
             {
                 Debugger.Break();
                 return null;
             }
        }

        private void lstValues_ParentChanged(object sender, EventArgs e)
        {
            ListBox lstValues = (ListBox)sender;

            if (lstValues.Parent != null)
            {
                lstValues.Parent.Resize += new EventHandler(lstValues_Parent_Resize);
                lstValues.Parent.Move += new EventHandler(lstValues_Parent_Move);
            }
        }

        private void lstValues_Parent_Move(object sender, EventArgs e)
        {
            Control ctrlParent = (Control)sender;

            m_rcList = ctrlParent.Bounds;
        }

        private void lstValues_Parent_Resize(object sender, EventArgs e)
        {
            Control ctrlParent = (Control)sender;

            m_rcList = ctrlParent.Bounds;
        }

        private void SelectedIndexChanged(object sender, EventArgs e)
        {
            ListBox lst = (ListBox)sender;
            IWindowsFormsEditorService editorService = (IWindowsFormsEditorService) lst.Tag;

            editorService.CloseDropDown();
        }
    }
}
